package com.ccrfid.rmc6.resource.business.prison.rollcall;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.business.prison.rollcall.PrisonRollcallPlaceEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelView;
import com.ccrfid.rmc6.pojo.dto.prison.rollcall.PrisonRollcallPersonnelDto;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.business.prison.rollcall.PrisonRollcallPlaceService;
import com.ccrfid.rmc6.service.personnel.PersonnelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

@Path("prison-rollcall/places")
@Produces(MediaType.APPLICATION_JSON)
public class PrisonRollcallPlaceResource {
    private static final Logger logger = LoggerFactory.getLogger(PrisonRollcallPlaceResource.class);

    @Autowired
    private PrisonRollcallPlaceService rollcallPlaceService;
    @Autowired
    private PersonnelService personnelService;

    //获取点名地点列表
    @GET
    public List<PrisonRollcallPlaceEntity> getRollcallPlaceList() {
        return rollcallPlaceService.getRollcallPlaceList();
    }

    //获取单个点名地点
    @GET
    @Path("{placeId}")
    public PrisonRollcallPlaceEntity getRollcallPlace(@PathParam("placeId") Long placeId) {
        return rollcallPlaceService.getRollcallPlace(placeId);
    }

    //新增点名地点信息
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public PrisonRollcallPlaceEntity createRollcallPlace(PrisonRollcallPlaceEntity entity) throws AppException {
        return rollcallPlaceService.createRollcallPlace(entity);
    }

    //修改点名地点信息
    @PUT
    @Path("{placeId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public PrisonRollcallPlaceEntity updateRollcallPlace(@PathParam("placeId") Long placeId,
                                                         PrisonRollcallPlaceEntity entity) throws AppException {
        return rollcallPlaceService.updateRollcallPlace(placeId, entity);
    }

    //删除点名地点信息
    @DELETE
    @Path("{placeId}")
    public Boolean deleteRollcallPlace(@PathParam("placeId") Long placeId) {
        return rollcallPlaceService.deleteRollcallPlace(placeId);
    }

    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean deleteRollcallPlaceList(List<Long> idList) throws AppException {
        if (idList == null) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.param_is_null.getCode(), CustomErrors.param_is_null.getReason(), "");
        }
        return rollcallPlaceService.deleteRollcallPlaceList(idList);
    }

    //获取与某个点名地点关联的被点人员列表
    @GET
    @Path("{placeId}/personnels")
    public List<PrisonRollcallPersonnelDto> getRollcallPlacePersonnelList(@PathParam("placeId") Long placeId) {
        return rollcallPlaceService.getRollcallPlacePersonnelList(placeId);
    }

    //分页获取与某个点名地点关联的被点人员列表
    @GET
    @Path("{placeId}/personnels/search")
    public Page<PersonnelView> getRollcallPlacePersonnelPage(@PathParam("placeId") Long placeId,
                                                             @QueryParam("personnelName") String personnelName,
                                                             @QueryParam("nameAcronym") String nameAcronym,
                                                             @QueryParam("personnelSn") String personnelSn,
                                                             @QueryParam("cardNumber") Long cardNumber,
                                                             @BeanParam PageParam pageParam) {
        List<Long> personnelIdList = rollcallPlaceService.getRollcallPlacePersonnelIdList(placeId);
        if (personnelIdList != null && personnelIdList.size() > 0) {
            SearchCriteria criteria = SearchCriteria.Builder.create();
            criteria.add(new Criterion("id", Criterion.Operator.IN,  personnelIdList));

            if (personnelName != null) {
                criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, '%' + personnelName + '%'));
            }
            if (nameAcronym != null) {
                criteria.add(new Criterion("nameAcronym", Criterion.Operator.LIKE, '%' + nameAcronym + '%'));
            }
            if (personnelSn != null) {
                criteria.add(new Criterion("personnelSn", Criterion.Operator.LIKE, '%' + personnelSn + '%'));
            }
            if (cardNumber != null) {
                criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
            }
            PageRequest pageRequest;
            if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
                pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
            } else {
                pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
            }
            Page<PersonnelView> page = personnelService.getPersonnelInfoPage(criteria, pageRequest);
            return page;
        } else {
            PageRequest pageRequest;
            if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
                pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
            } else {
                pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
            }
            return new PageImpl<>(new ArrayList<>(), pageRequest, 0);
        }
    }

    //修改某个地点关联的被点名人员
    @PUT
    @Path("{placeId}/personnels")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean updateRollcallPlacePersonnelList(@PathParam("placeId") Long placeId, List<Long> personnelIdList)
            throws AppException {
        return rollcallPlaceService.updateRollcallPlacePersonnelList(placeId, personnelIdList);
    }

    //为某个点名地点添加关联的被点名人员
    @POST
    @Path("{placeId}/personnels")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean createRollcallPlacePersonnelList(@PathParam("placeId") Long placeId, List<Long> personnelIdList)
            throws AppException {
        return rollcallPlaceService.createRollcallPlacePersonnelList(placeId, personnelIdList);
    }

    //为某个点名地点删除关联的被点名人员
    @DELETE
    @Path("{placeId}/personnels/{personnelId}")
    public Boolean deleteRollcallPlacePersonnel(@PathParam("placeId") Long placeId,
                                                @PathParam("personnelId") Long personnelId) throws AppException {
        return rollcallPlaceService.deleteRollcallPlacePersonnel(placeId, personnelId);
    }

    @DELETE
    @Path("{placeId}/personnels")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean deleteRollcallPlacePersonnelList(@PathParam("placeId") Long placeId, List<Long> personnelIdList)
            throws AppException {
        return rollcallPlaceService.deleteRollcallPlacePersonnelList(placeId, personnelIdList);
    }

    //获取某个部门下未被某个点名地点关联的被点名人员
    @GET
    @Path("{placeId}/free-personnels-in-org/{orgId}")
    public List<PersonnelView> getRollcallPlaceFreePersonnelsInOrg(@PathParam("placeId") Long placeId,
                                                                   @PathParam("orgId") Long orgId) {
        return rollcallPlaceService.getRollcallPlaceFreePersonnelsInOrg(placeId, orgId);
    }
}
